package com.itcast.yygh.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.common.result.R;
import com.itcast.yygh.model.model.user.UserInfo;
import com.itcast.yygh.model.vo.user.UserInfoQueryVo;
import com.itcast.yygh.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "用户后台管理")
@Slf4j
@RestController
@RequestMapping("/admin/userinfo")
@RequiredArgsConstructor
public class UserAdminController {
    private final UserInfoService userInfoService;

    //分页条件查询用户列表
    @ApiOperation("分页条件查询用户列表")
    @GetMapping("{page}/{limit}")
    public R findPage(@PathVariable Long page,
                      @PathVariable Long limit,
                      UserInfoQueryVo userInfoQueryVo) {
        Page<UserInfo> pageParam = new Page<>(page, limit);
        IPage<UserInfo> pageModel =
                userInfoService.selectPage(pageParam, userInfoQueryVo);
        return R.ok().data("pageModel", pageModel);
    }

    //用户数据锁定和解锁功能
    @ApiOperation("用户数据锁定和解锁接口")
    @GetMapping("lock/{userId}/{status}")
    public R lock(
            @PathVariable("userId") Long userId,
            @PathVariable("status") Integer status) {
        try {
            userInfoService.lock(userId, status);
            return R.ok();
        } catch (Exception e) {
            log.error(e.toString());
            return R.error().message(e.toString());
        }
    }

    //用户详情
    @ApiOperation("查看用户详情")
    @GetMapping("show/{userId}")
    public R show(@PathVariable Long userId) {
        try {
            Map<String, Object> map = userInfoService.show(userId);
            return R.ok().data(map);
        }catch (Exception e) {
            log.error(e.toString());
            return R.error().message(e.toString());
        }
    }

    //认证审批
    @ApiOperation("认证审批")
    @GetMapping("approval/{userId}/{authStatus}")
    public R approval(@PathVariable Long userId,@PathVariable Integer authStatus) {
        userInfoService.approval(userId,authStatus);
        return R.ok();
    }

}
